package edu.stu.book.book.keeping.ui.screen.home.add

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.Icon
import androidx.compose.material.ModalBottomSheetLayout
import androidx.compose.material.ModalBottomSheetValue
import androidx.compose.material.Text
import androidx.compose.material.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.key
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import edu.stu.book.book.keeping.R
import edu.stu.book.book.keeping.component.CalculatorKeyboard
import edu.stu.book.book.keeping.db.dao.CostType
import edu.stu.book.book.keeping.db.dao.costTypes
import edu.stu.book.book.keeping.extensions.popTip
import edu.stu.book.book.keeping.utils.ColorUtils
import edu.stu.book.book.keeping.utils.CostUtils
import kotlinx.coroutines.launch

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun CostScreen(
    modifier: Modifier = Modifier
) {

    val context = LocalContext.current

    val state = rememberModalBottomSheetState(ModalBottomSheetValue.Hidden)
    val scope = rememberCoroutineScope()
    var selectIndex by rememberSaveable { mutableIntStateOf(-1) }

    ModalBottomSheetLayout(
        sheetState = state,
        sheetContent = { CalculatorKeyboard(context = context) },
        modifier = modifier.padding(bottom = 60.dp)
    ) {

        LazyVerticalGrid(
            columns = GridCells.Fixed(4), // 每行固定4列
            modifier = Modifier.fillMaxSize(),
            contentPadding = PaddingValues(8.dp)
        ) {
            items(costTypes.size) { index ->
                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    verticalArrangement = Arrangement.Center,
                    modifier = Modifier
                        .padding(8.dp)
                        .fillMaxWidth()
                        .height(60.dp)
                        .background(
                            if (index == selectIndex) ColorUtils.getColor(
                                context,
                                R.color.icon_select_color
                            ) else Color.Transparent,
                            RoundedCornerShape(55.dp)
                        )
                        .clickable(
                            indication = null,
                            interactionSource = remember { MutableInteractionSource() }
                        ) {
                            selectIndex = index
                            scope.launch { state.show() }
                            "单击了${CostUtils.getCostTypeText(costTypes[index])}".popTip()
                        },
                ) {
                    // 显示图标
                    Icon(
                        painter = painterResource(
                            id = CostUtils.getCostTypeIcon(
                                costTypes[index]
                            )
                        ),
                        contentDescription = CostUtils.getCostTypeText(costTypes[index]),
                        modifier = Modifier.size(24.dp),
                        tint = ColorUtils.getColor(context)
                    )
                    // 显示文本
                    Text(
                        text = CostUtils.getCostTypeText(costTypes[index]),
                        fontSize = 16.sp,
                        color = ColorUtils.getColor(context)
                    )
                }
            }
        }
    }
}

@Preview(name = "CostScreen")
@Composable
private fun PreviewCostScreen() {
    CostScreen()
}